package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.domain.CourseUserLearn;
import cn.itsource.ymcc.dto.CourseDTO;
import cn.itsource.ymcc.service.ICourseService;
import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.query.CourseQuery;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.vo.CourseDetailVO;
import cn.itsource.ymcc.vo.CourseOrderDetailVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;



    /**
     * 保存和修改公用的
     */
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }


    /**
     * 保存和修改公用的
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody @Valid CourseDTO courseDTO){
        courseService.save(courseDTO);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/saveOrUpdate",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.insert(course);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/detail/data/{courseId}",method = RequestMethod.GET)
    public JSONResult getDetail(@PathVariable("courseId")Long courseId){
        CourseDetailVO courseDetailVO = courseService.getDetail(courseId);
        return JSONResult.success(courseDetailVO);
    }


    /**
     * 获取对象
     */
    @RequestMapping(value = "/info/{courseids}",method = RequestMethod.GET)
    public JSONResult getOrderDetail(@PathVariable("courseids")String courseIds){
        CourseOrderDetailVo courseOrderDetailVO = courseService.getOrderDetail(courseIds);
        return JSONResult.success(courseOrderDetailVO);
    }

    /**
     * 获取对象
     */
    //@PreAuthorize("hasAuthority('course:getCourseforuser')")
    @RequestMapping(value = "/courseforuser/{courseId}/{loginId}",method = RequestMethod.GET)
    public JSONResult getCourseforuser(@PathVariable("courseId")Long courseId,@PathVariable("loginId")Long loginId){
        CourseUserLearn courseforuser = courseService.getCourseforuser(courseId,loginId);
        return JSONResult.success(courseforuser);
    }


    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @PreAuthorize("hasAuthority('course:list')")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> wrapper = new EntityWrapper();
        wrapper.like("name",query.getKeyword()).or().like("grade_name",query.getKeyword()); // select * from t_course where name like %keyword% or grated like %keyword%
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
